home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Applications / NeuroSim 1.0 / .cp / CStdNeuron.cp < prev    next >
Text File  |  1996-02-19  |  4KB  |  135 lines

  1. // ===========================================================================
  2. //    CStdNeuron.cp                ©1996 Timo Eloranta
  3. // ===========================================================================
  4. //  A concrete neuron class derived from the abstract CNeuron class
  5.  
  6. #include "CStdNeuron.h"
  7. #include "CNeuralNet.h"
  8. #include "NS_Utils.h"                    // PlayOneSnd
  9.  
  10. #include <LListIterator.h>                // PowerPlant header
  11.  
  12. // ---------------------------------------------------------------------------
  13. //        • DoClickAction
  14. //
  15. //          Called by:    CNeuroSimPane::ClickSelf
  16. //                        CNeuralNet::SpendTime
  17. // ---------------------------------------------------------------------------
  18. //    React to a mouse click inside this neuron. If the neuron is
  19. //    a receptor (and the option key is not pressed) we add the neuron
  20. //    to the "light up queue" and process the queue right away. 
  21. //    With "ordinary" neurons (and receptors when option key is pressed!)
  22. //    we show the user the neurons which are connected to this neuron by
  23. //    coloring the connections and by marking the "destination" neurons
  24. //    with a yellow ring.
  25.  
  26. void
  27. CStdNeuron::DoClickAction( Boolean inOptionKeyDown )
  28. {
  29.     LListIterator    theScanner( mNextOnes, iterate_FromStart );
  30.     CNeuronPtr        theNext;
  31.  
  32.     if ( IsReceptor() && !inOptionKeyDown ) {
  33.     
  34.         PlayOneSnd( snd_Click, true );
  35.         SetNeuronState( mMaxState + 1 );        // To get the right color
  36.         sNet -> AddToLightQ( (LLink *) this );
  37.         sNet -> ProcessLightQ();
  38.         
  39.     } else {
  40.     
  41.         // Color the connections and show the connected neurons
  42.         
  43.         while ( theScanner.Next( &theNext ))
  44.             theNext -> SetDestinationFlag( true );
  45.         
  46.         mConnDirty = mConnLit = true;
  47.         sNet -> SetNeuronsDirty( mConnRect );
  48.         sNet -> RequestDraw();
  49.         
  50.         // Hold on as long as the mouse button is pushed
  51.         
  52.         while ( ::StillDown() ) { };
  53.         
  54.         // Restore the drawing
  55.  
  56.         theScanner.ResetTo( iterate_FromStart );
  57.         while ( theScanner.Next( &theNext ))
  58.             theNext -> SetDestinationFlag( false );
  59.         
  60.         mConnDirty    = true;
  61.         mConnLit    = false;
  62.         sNet -> SetNeuronsDirty( mConnRect );
  63.         sNet -> RequestDraw();
  64.     };
  65. }
  66.  
  67. // ---------------------------------------------------------------------------
  68. //        • ShouldLightUp
  69. //
  70. //          Called by:    CNeuron::IncState
  71. // ---------------------------------------------------------------------------
  72. //    Return true if the state of this neuron is such that it should
  73. //    be in the "light up queue".
  74.  
  75. inline Boolean
  76. CStdNeuron::ShouldLightUp() const
  77. {
  78.     return mState > mMaxState;
  79. }
  80.  
  81. // ---------------------------------------------------------------------------
  82. //        • DoLightUpAction
  83. //
  84. //          Called by:    CNeuralNet::ProcessLightQ
  85. // ---------------------------------------------------------------------------
  86. //    Here we define what happens when a neuron lights up.
  87.  
  88. void
  89. CStdNeuron::DoLightUpAction()
  90. {
  91.     LListIterator    theScanner( mNextOnes, iterate_FromStart );
  92.     CNeuronPtr        theNext;
  93.  
  94.     // Force the connections starting from this neuron
  95.     // to be redrawn with some bright color (yellow).
  96.  
  97.     mConnDirty = mConnLit = true;
  98.  
  99.     // Force the surrounding neurons to be redrawn
  100.     // because drawing connections might mess them up
  101.  
  102.     sNet -> SetNeuronsDirty( mConnRect );
  103.     
  104.     // Increase the state of the connected nodes
  105.     
  106.     while ( theScanner.Next( &theNext ) )
  107.         theNext -> IncState();
  108. }
  109.  
  110. // ---------------------------------------------------------------------------
  111. //        • SetPostLightUpState
  112. //
  113. //          Called by:    CNeuralNet::ProcessLightQ
  114. // ---------------------------------------------------------------------------
  115. //    Here we define what happens when a neuron fades back to normal
  116. //    after lighting up.
  117.  
  118. void
  119. CStdNeuron::SetPostLightUpState()
  120. {
  121.     // Restore the normal color to the connections
  122.  
  123.     mConnDirty    = true;
  124.     mConnLit    = false;
  125.  
  126.     // The surrounding neurons have to be redrawn
  127.     // because drawing connections might mess them up...
  128.  
  129.     sNet -> SetNeuronsDirty( mConnRect );
  130.  
  131.     // And finally... set the state of this neuron back to 0.
  132.  
  133.     SetNeuronState( 0 );
  134. }
  135.